{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (c) MONAI Consortium  \n",
    "Licensed under the Apache License, Version 2.0 (the \"License\");  \n",
    "you may not use this file except in compliance with the License.  \n",
    "You may obtain a copy of the License at  \n",
    "&nbsp;&nbsp;&nbsp;&nbsp;http://www.apache.org/licenses/LICENSE-2.0  \n",
    "Unless required by applicable law or agreed to in writing, software  \n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,  \n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  \n",
    "See the License for the specific language governing permissions and  \n",
    "limitations under the License. \n",
    "\n",
    "# MONAI Label VISTA2D App - CellProfiler Cell Profile Example\n",
    "\n",
    "***The cell bioimage profile with MONAI Label***\n",
    "\n",
    "In this notebook, we show the example of MONAI Label VISTA2D usecase. The cell profile is demonstrated in this notebook. This tutorial uses CellProfiler as the client viewer, and we demonstrate the end-to-end workflow with MONAI Label + CellProfiler integration.\n",
    "\n",
    "![cellprofiler1](./figures/monailabel_vista_cellprofiler/cellprofiler_open.jpg)\n",
    "\n",
    "## 1. Prepare MONAI Label"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup environment\n",
    "\n",
    "### Prerequisites\n",
    "- **Install MONAI Label** weekly preview release: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install monailabel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 Download VISTA2D app\n",
    "Choose \"vista2d\" as the app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!monailabel apps --download --name vista2d --output apps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 Download sample data\n",
    "Download the [cellpose dataset](https://www.cellpose.org/dataset) and save to the **datasets** folder for further use. Please convert the image format from `png` to `tiff` before loading to the CellProfiler. Here is a short code for reference:\n",
    "```python\n",
    "from PIL import Image\n",
    "image_path = \"/path/to/your/image.png\"\n",
    "tif_path = \"/path/to/save/image.tiff\"\n",
    "img = Image.open(image_path)\n",
    "img.save(tif_path)\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 Starting MONAI Label Server\n",
    "\n",
    "The command will start MONAI Label server with VISTA2D.\n",
    "\n",
    "```bash\n",
    "monailabel start_server --app apps/vista2d --studies datasets --conf models vista2d --conf preload true --conf skip_trainers true\n",
    "```\n",
    "In the command, ```--app``` specifies the VISTA2D app path, ```--studies``` specifies the dataset folder path.\n",
    "\n",
    "**Note:** If you are running a MONAI Label server in this notebook and plan to close the notebook, ensure that you terminate the kernel first. Failing to do so might result in an issue where the port remains occupied from the previous session when you try to restart the server later. To prevent this, always terminate the kernel before closing the notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Install CellProfiler and MONAI Label Plugin\n",
    "\n",
    "\n",
    "[CellProfiler](https://cellprofiler.org/) is a free open-source software designed to enable biologists without training in computer vision or programming to quantitatively measure phenotypes from thousands of images automatically.\n",
    "\n",
    "Note: Nvidia does not maintain the thrid-party visualization tools. Please refer to installation part in the [CellProfiler WiKi](https://github.com/CellProfiler/CellProfiler/wiki) for details or trouble shooting. \n",
    "\n",
    "After installed the CellProfiler, please download the [plugin python file](https://github.com/Project-MONAI/MONAILabel/blob/main/plugins/cellprofiler/runvista2d.py) and add the path to the file to `File-->preferences-->CellProfiler plugin directory`. Please add the folder path instead of the file path.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Cell Profile with CellProfiler\n",
    "\n",
    "Below is a step-by-step tutorial for cell profile using CellProfiler. Pre-trained model is provided and loaded by MONAI Label server. We prepared an example pipeline named `cellprofiler_demo_pipeline.cpproj` in the extra_files folder to illustrate the use case. Feel free to update it according your own requirements.\n",
    "\n",
    "\n",
    "### 3.1 Start CellProfiler and Load the Example Pipleline\n",
    "\n",
    "- Once finished the installation, drag the example pipeline to the CellProfiler pipeline window. Then the pipeline will be loaded into the screen as shown in the red box.\n",
    "\n",
    "<p align = \"center\"><img src=\"./figures/monailabel_vista_cellprofiler/cellprofiler_pipeline.jpg\" alt=\"drawing\" width=\"800\"/></p>\n",
    "\n",
    "### 3.2 Start CellProfiler and Load the Example Pipleline\n",
    "\n",
    "- Load your local samples to CellProfiler by dropping the sample folder or samples.\n",
    "\n",
    "<p align = \"center\"><img src=\"./figures/monailabel_vista_cellprofiler/cellprofiler_samples.jpg\" alt=\"drawing\" width=\"800\"/></p>\n",
    "\n",
    "### 3.3 Run the CellProfiler Pipeline and Edit the Segmentation Mask\n",
    "\n",
    "- After loading samples, click the `Analyse Images` button to start the profile. The demo pipeline provide the option to edit the output mask. Then the output profile will be generated based on the edited mask.\n",
    "\n",
    "<p align = \"center\"><img src=\"./figures/monailabel_vista_cellprofiler/cellprofiler_edit.jpg\" alt=\"drawing\" width=\"800\"/></p>\n",
    "\n",
    "<p align = \"center\"><img src=\"./figures/monailabel_vista_cellprofiler/cellprofiler_run.jpg\" alt=\"drawing\" width=\"800\"/></p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Conclusion\n",
    "\n",
    "This tutorial demonstrates the basic usage of MONAI Label with the VISTA2D app, the cell profile task is introduced. We provided an example pipeline to show case the whole workflow. \n",
    "\n",
    "In the VISTA2D usecase, we highlight the profile pipeline with the CellProfiler on cell bioimages. \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
